﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SinsenWeb.Helpers
{
    public class MessageHelper
    {
        private const string ALERTS = "Alerts";
        private const string MESSAGES = "Messages";

        public static void Message(string message)
        {
            var messages = GetSessionList(MESSAGES);
            if (messages == null)
                return;
            messages.Add(message);
        }

        public static void Alert(string message)
        {
            var alerts = GetSessionList(ALERTS);
            if (alerts == null)
                return;
            alerts.Add(message);
        }

        public static List<string> GetAndClearMessages()
        {
            var oldMessages = GetSessionList(MESSAGES);
            if (oldMessages == null)
                return new List<string>();
            ResetSessionList(MESSAGES);
            return oldMessages;
        }

        public static List<string> GetAndClearAlerts()
        {
            var oldMessages = GetSessionList(ALERTS);
            if (oldMessages == null)
                return new List<string>();
            ResetSessionList(ALERTS);
            return oldMessages;
        }

        private static void ResetSessionList(string listId)
        {
            if (HttpContext.Current == null
                || HttpContext.Current.Session == null)
            {
                return;
            }
            var session = HttpContext.Current.Session;
            session[listId] = new List<string>();
        }

        private static List<string> GetSessionList(string listId)
        {
            if (HttpContext.Current == null
                || HttpContext.Current.Session == null)
            {
                return null;
            }
            var session = HttpContext.Current.Session;
            if (session[listId] == null)
            {
                session[listId] = new List<string>();
            }
            return session[listId] as List<string>;
        }

        public static class DefaultStrings
        {
            public static string NeedLogin = "Du må være logget inn for å se dette.";
            public static string NeedAdmin = "Du må være logget inn som administrator for å se/gjøre dette";
            public static string NeedEditor = "Du må være logget inn som nettredaktør for å se/gjøre dette";
        }
    }
}
